<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-s-custom"></i> 管理员管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button
                    type="primary"
                    icon="el-icon-delete"
                    class="handle-del mr10"
                    @click="delAllSelection"
                >批量删除</el-button>
                <el-input v-model="query.username" placeholder="用户名" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <el-button type="danger" icon="el-icon-plus" @click="handleAdd">新增</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="username" label="用户名"></el-table-column>
                <el-table-column prop="password" label="密码" ></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handlePageChange"
                    :current-page="query.pageIndex"
                    :page-sizes="[10, 50, 100, 500]"
                    :page-size=query.pageSize
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="pageTotal">
                </el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog :title="title" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="用户名">
                    <el-input v-model="form.username"></el-input>
                </el-form-item>
                <el-form-item label="密码">
                    <el-input v-model="form.password"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="done">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    name: 'testTable',
    data() {
        return {
            title: "",
            query: {
                username: '',
                pageIndex: 1,
                pageSize: 10
            },
            tableData: [],
            multipleSelection: [],
            editVisible: false,
            pageTotal: 0,
            form: {},
        };
    },
    created() {
        this.getData();
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        getData() {
            this.$http.get('/admin',{
                params: {
                    currentPage: this.query.pageIndex,
                    pageSize: this.query.pageSize,
                    keyword: this.query.username
                }}).then((res)=>{//回调函数
                    this.tableData = res.data.records
                    this.pageTotal = res.data.totalElements;
            })
        },
        // 触发搜索按钮
        handleSearch() {
            console.log(this.query);
            this.$set(this.query, 'pageIndex', 1);
            this.getData();
        },
        // 新增操作
        handleAdd(){
            this.form = {};
            this.title = "新增管理员";
            this.editVisible = true;
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    this.$http.delete('/admin/' + row.id).then((res)=>{//回调函数
                        if (res.data.code === 200) {
                            this.getData();
                            this.$message.success('删除成功');
                        }else {
                            this.$message.error(res.data.msg);
                        }
                    })
                    this.tableData.splice(index, 1);
                })
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            let ids = this.multipleSelection.map(item => item.id)
            this.$http.delete('/admin/deleteBatch', {data: ids}).then((res)=>{//回调函数

                this.$message.error(`删除了` + res.data.data + '条数据');
                this.getData();

        })

            this.multipleSelection = [];
        },
        // 编辑操作
        handleEdit(index, row) {
            this.title = "编辑"
            //生成row的副本，防止修改原数据
            this.form = Object.assign({}, row);
            this.editVisible = true;
        },
        //点击确定按钮判断是保存还是更新
        done(){
            if(this.form.id){//有id就是更新
                this.edit()
            }else {
                this.save()
            }
        },
        // 新增
        save() {
            this.$http.post("/admin", this.form)
                .then((res)=>{
                    if (res.data.code == 200) {
                        this.editVisible = false;

                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                        this.getData();
                    }else {
                        this.$message({
                            message: res.data.message,
                            type: 'error'
                        });
                    }
                })
        },
        // 编辑
        edit() {
            this.$http.put("/admin/updateOne", this.form)
                .then((res)=>{
                    if (res.data.code == 200) {
                        this.editVisible = false;

                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                        this.getData();
                    }else {
                        this.$message({
                            message: res.data.message,
                            type: 'error'
                        });
                    }
                })
        },
        // 每页条数更改
        handleSizeChange(val) {
            this.$set(this.query, 'pageIndex', 1);
            this.$set(this.query, 'pageSize', val);
            this.getData();
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-size{
    width: 70px;
}

.handle-select {
    width: 120px;
}

.handle-title{
    margin-left: 20px;
    margin-right: 10px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
